﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BoAddonBiz_FreightForwarder.Cost.OpenInvoice
{
    abstract class B1Form_OpenInvoice:SAPBoAddon.B1Form
    {
        #region abstract
        protected abstract string MasterTable { get; }
        protected abstract string SubTable { get;  }
        #endregion
        #region private variable
        string DocEntry = string.Empty;
        bool Flag = false;
        #endregion
        #region protected override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            #region 控件是否可编辑
            FormSetting.FormModeSetting.StatusItemUID = "7";
            SAPBoAddon.BrowseModeSetting ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "O";
            ModeSetting.itmBrowseModeDisable = new string[] { "3" }; 
            ModeSetting.itmBrowseModeEnable = Utils.GetFormItems(FormSetting.FormXmlDocument);

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "C";
            ModeSetting.itmBrowseModeDisable = Utils.GetFormItems(FormSetting.FormXmlDocument);
          
            FormSetting.FormModeSetting.itmFindModeEnable = new string[] { "3" };
            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            if (new Regex(@"\b\d+\b").Match(InitPar.FatherFormUID).Success)
            {
                this.CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                SAPBoAddon.B1Assistant.Item.SetValue(this.CurrentForm, this.CurrentForm.DataBrowser.BrowseBy, InitPar.FatherFormUID);
                this.CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);

                Utils.SetMatrixRowEnable(SBOApp.Forms.ActiveForm, "mtx_0");
                Utils.SetMatrixRowDisable(SBOApp.Forms.ActiveForm, "mtx_0", "col_17", "C");
            }
        }
        #endregion
        #region constractor
        public B1Form_OpenInvoice()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_OpenInvoice_frm_ItemEvent);
            this.frm_RightClickEvent += new SAPBoAddon.IB1FormEvents.B1RightClickEventEventHandler(B1Form_OpenInvoice_frm_RightClickEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_OpenInvoice_frm_MenuEvent);
        }
        #endregion
        #region events
        void B1Form_OpenInvoice_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.MenuUID == "Modify")
                {
                    Modify();
                }
                if (pVal.MenuUID == "ModifyOrder")
                {
                    ModifyOrder();
                }
                if (pVal.MenuUID == "Close")
                {
                    Close();
                }
            }   
            else
            {
                //移除菜单
                RemoveRightClickMenus();
            }
        }
        void B1Form_OpenInvoice_frm_RightClickEvent(SAPBoAddon.B1AddonBase.B1ContextMenuInfo eventInfo, ref bool BubbleEvent)
        {
            if (eventInfo.BeforeAction)
            {
                if (eventInfo.EventType == SAPbouiCOM.BoEventTypes.et_RIGHT_CLICK)
                {
                    //添加菜单
                    AddRightClickMenus();
                }
            }
        }

        void B1Form_OpenInvoice_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == SAPbouiCOM.BoFormMode.fm_UPDATE_MODE)
                    {
                        DocEntry = SAPBoAddon.B1Assistant.Item.GetValue(pVal.FormUID, "3").ToString();
                        Flag = true;
                    }
                }
                if (pVal.ItemUID == "40" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "DocType").ToString() == "I")
                    {
                        SBOApp.OpenForm(SAPbouiCOM.BoFormObjectEnum.fo_Invoice, "133", SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "BaseEntry").ToString());
                        BubbleEvent = false;
                    }
                }
            }
            else
            {
                if (pVal.ItemUID == "1" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == SAPbouiCOM.BoFormMode.fm_OK_MODE && Flag)
                    {
                        Flag = false;
                        CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                        SAPBoAddon.B1Assistant.Item.SetEnabled(pVal.FormUID, "3", true);
                        SAPBoAddon.B1Assistant.Item.SetValue(pVal.FormUID, "3", DocEntry);
                        CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);

                        Utils.SetMatrixRowEnable(SBOApp.Forms.ActiveForm, "mtx_0");
                        Utils.SetMatrixRowDisable(SBOApp.Forms.ActiveForm, "mtx_0", "col_17", "C");
                    }
                }
                if (pVal.ItemUID == "mtx_0" && new string[] { "col_5", "col_6" }.Contains(pVal.ColUID) && pVal.ItemChanged)
                {
                    SAPbouiCOM.Matrix omtx = CurrentFormEx.Items.Item("mtx_0").Specific;
                    SAPBoAddon.B1Assistant.Matrix.SetCellValue(omtx, "col_7", pVal.Row, SAPBoAddon.B1Assistant.Matrix.GetCellValue(omtx, "col_4", pVal.Row));
                }
                if (pVal.ItemUID == "mtx_0" && pVal.ColUID == "col_7" && pVal.ItemChanged)
                {
                    sum(pVal.Row);
                }
                if (pVal.ItemUID == "UPD" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    UpdateInvoiceNo();
                }
            }
        }
        #endregion
        #region methods
        /// <summary>
        /// 添加右击菜单 
        /// </summary>
        /// <remarks></remarks>
        private void AddRightClickMenus()
        {
            SAPbouiCOM.Form oForm;
            oForm = CurrentForm;
            if (oForm.IsSystem) return;

            SAPbouiCOM.MenuItem oMenuItem = default(SAPbouiCOM.MenuItem);
            SAPbouiCOM.Menus oMenus = default(SAPbouiCOM.Menus);
            SAPbouiCOM.MenuCreationParams oCreationPackage = default(SAPbouiCOM.MenuCreationParams);
            string Status = string.Empty;
            try
            {
                oCreationPackage = SBOApp.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_MenuCreationParams);
                oCreationPackage.Type = SAPbouiCOM.BoMenuType.mt_STRING;
                oCreationPackage.UniqueID = "Modify";
                oCreationPackage.String = "修改行";
                oCreationPackage.Enabled = true;

                oMenuItem = SBOApp.Menus.Item("1280");
                oMenus = oMenuItem.SubMenus;
                if (!oMenus.Exists("Modify"))
                {
                    oMenus.AddEx(oCreationPackage);
                }

                oCreationPackage = SBOApp.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_MenuCreationParams);
                oCreationPackage.Type = SAPbouiCOM.BoMenuType.mt_STRING;
                oCreationPackage.UniqueID = "ModifyOrder";
                oCreationPackage.String = "修改单据";
                oCreationPackage.Enabled = true;

                oMenuItem = SBOApp.Menus.Item("1280");
                oMenus = oMenuItem.SubMenus;
                if (!oMenus.Exists("ModifyOrder"))
                {
                    oMenus.AddEx(oCreationPackage);
                }

                oCreationPackage = SBOApp.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_MenuCreationParams);
                oCreationPackage.Type = SAPbouiCOM.BoMenuType.mt_STRING;
                oCreationPackage.UniqueID = "Close";
                oCreationPackage.String = "结算";
                oCreationPackage.Enabled = true;

                oMenuItem = SBOApp.Menus.Item("1280");
                oMenus = oMenuItem.SubMenus;
                if (!oMenus.Exists("Close"))
                {
                    oMenus.AddEx(oCreationPackage);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 移除右击菜单 
        /// </summary>
        /// <remarks></remarks>
        private void RemoveRightClickMenus()
        {
            try
            {
                if (SBOApp.Menus.Exists("Modify"))
                {
                    SBOApp.Menus.RemoveEx("Modify");
                }
                if (SBOApp.Menus.Exists("ModifyOrder"))
                {
                    SBOApp.Menus.RemoveEx("ModifyOrder");
                }
                if (SBOApp.Menus.Exists("Close"))
                {
                    SBOApp.Menus.RemoveEx("Close");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Modify()
        {
            SAPbouiCOM.Matrix omtx = CurrentForm.Items.Item("mtx_0").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(MasterTable);
            SAPbouiCOM.DBDataSource dbds1 = CurrentForm.DataSources.DBDataSources.Item(SubTable);
            if (dbds.GetValue("Status", dbds.Offset).Trim() == "C")
            {
                SBOApp.SetStatusBarMessage("此单据已关闭，不能进行修改。");
                return;
            }
            int SelectRow = omtx.GetNextSelectedRow();
            if (SelectRow > 0)
                dbds.SetValue("U_IsModify", dbds.Offset, "Y");
            while (SelectRow > 0)
            {
                dbds1.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(omtx, SelectRow, "col_f", dbds1);
                dbds1.SetValue("U_Status", dbds1.Offset, "O");
                omtx.SetLineData(SelectRow);
                omtx.SelectRow(SelectRow, false, false);
                SelectRow = omtx.GetNextSelectedRow();
            }
            Utils.SetMatrixRowEnable(SBOApp.Forms.ActiveForm, "mtx_0");
            Utils.SetMatrixRowDisable(SBOApp.Forms.ActiveForm, "mtx_0", "col_17", "C");
        }
        private void ModifyOrder()
        {
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(MasterTable);
            if (dbds.GetValue("Status", dbds.Offset).Trim() == "C" && dbds.GetValue("Canceled", dbds.Offset).Trim() == "N")
            {
                dbds.SetValue("Status", dbds.Offset, "O");
                if(CurrentForm.Mode != SAPbouiCOM.BoFormMode.fm_UPDATE_MODE)CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE;
                CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
                //string DocEntry = dbds.GetValue("DocEntry", dbds.Offset).Trim();
                //CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                //SAPBoAddon.B1Assistant.Item.SetEnabled(CurrentForm,"3", true);
                //SAPBoAddon.B1Assistant.Item.SetValue(CurrentForm, "3", DocEntry);
                //CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
            }
            else
            {
                SBOApp.MessageBox("此单据不能进行此操作");
            }
        }
        private void Close()
        {
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item(MasterTable);
            if (dbds.GetValue("Status", dbds.Offset).Trim() == "O" && dbds.GetValue("Canceled", dbds.Offset).Trim() == "N")
            {
                dbds.SetValue("Status", dbds.Offset, "C");
                if (CurrentForm.Mode != SAPbouiCOM.BoFormMode.fm_UPDATE_MODE) CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE;
                if(SBOApp.MessageBox("是否要关闭此单据？",2,"是","否") == 1)
                CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
            }
            else
            {
                SBOApp.MessageBox("此单据不能进行此操作");
            }
        }
        private void sum(int row)
        {
            string Amount = "0";
            SAPbouiCOM.Matrix omtx = CurrentFormEx.Items.Item("mtx_0").Specific;
            for (int i = 1; i <= omtx.VisualRowCount; i++)
            {
                string RowAmount = SAPBoAddon.B1Assistant.Matrix.GetCellValue(omtx, "col_7", i).ToString();
                Amount = (Convert.ToDecimal(Amount) + Convert.ToDecimal(RowAmount)).ToString();
            }
            SAPBoAddon.B1Assistant.Item.SetValue(CurrentFormEx.UniqueID, "InvoiTotal", Amount);
        }

        private void UpdateInvoiceNo()
        {
            string InvoiceNo = SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "InvoiNum").ToString();
            SAPbouiCOM.Matrix oMtx = CurrentForm.Items.Item("mtx_0").Specific;
            for (int i = 1; i <= oMtx.RowCount; i++)
            {
                if (SAPBoAddon.B1Assistant.Matrix.GetCellValue(oMtx, "col_10", i).ToString() == "Y")
                {
                    SAPBoAddon.B1Assistant.Matrix.SetCellValue(oMtx, "col_6", i, InvoiceNo);
                }
            }
            if (CurrentForm.Mode == SAPbouiCOM.BoFormMode.fm_OK_MODE)
            {
                CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE;
                SBOApp.MessageBox("更新完成，请保存单据！");
            }
        }
        #endregion
    }
}
